package com.fzrskj.cloud.free.user.utils.func;

import com.alibaba.fastjson.JSONObject;
import com.fzrskj.cloud.free.core.enums.CartTypeEnum;
import com.fzrskj.cloud.free.dic.utils.DicPriceDiyUtils;
import com.fzrskj.cloud.free.user.bean.UserAccount;
import com.fzrskj.cloud.free.user.bean.UserOrder;
import com.fzrskj.cloud.free.user.dao.vo.AgentFinancePriceVo;

import java.util.Map;

/**
 * FuncHandlePriceCloudAliYunHelper.
 *
 * @author zj.
 *         Created on 2019/9/2 0002.
 */
public class FuncHandlePriceVitrualDiyHelper {
    /**
     * 价格获取.
     * @param userOrder
     * @param accountAgent
     * @return
     */
    public static AgentFinancePriceVo getPrice(UserOrder userOrder, UserAccount accountAgent) {
        Map<String,Object> cdt = JSONObject.parseObject(userOrder.getOrderParam(), Map.class);
        cdt.put("productCode", userOrder.getProductCode());
        cdt.put("applyTime", userOrder.getApplyTime());
        cdt.put("priceType", userOrder.getPriceType());

        AgentFinancePriceVo priceVo = new AgentFinancePriceVo();

        DicPriceDiyUtils priceDiyUtils = new DicPriceDiyUtils();
        String priceInfo;
        //价格计算
        if(CartTypeEnum.ADD.equals(userOrder.getOrderType())) {
            //注册
            cdt.put("userId", accountAgent.getUserId());
            cdt.put("_agentId", accountAgent.getAgentId());
            cdt.put("chargeId", 1);
            priceInfo = priceDiyUtils.getPrice(cdt);
        } else if(CartTypeEnum.RENEW.equals(userOrder.getOrderType())) {
            //续费
            cdt.put("userId", accountAgent.getUserId());
            cdt.put("_agentId", accountAgent.getUserId());
            cdt.put("chargeId", 2);
            priceInfo = priceDiyUtils.getPrice(cdt);
        } else if (CartTypeEnum.UPDATE.equals(userOrder.getOrderType())) {
            priceVo.setAmount(-1.0);
            priceVo.setMessage("不支持的操作");
            return priceVo;
//            cdt.put("userId", userOrder.getUserId());
//            cdt.put("_agentId", accountAgent.getUserId());
//            //升级
//            priceInfo = priceDiyUtils.getUpgradePrice(cdt);
        } else {
            priceVo.setAmount(0.0);
            priceVo.setMessage("该操作免费");
            return priceVo;
        }

        if (null == priceInfo) {
            priceVo.setAmount(-1.0);
            priceVo.setMessage("获取代理价格失败");
            return priceVo;
        }
        JSONObject priceResult = JSONObject.parseObject(priceInfo);
        priceVo.setAmount(priceResult.getJSONObject("data").getDouble("payPrice"));
        priceVo.setMessage("获取代理价格成功");
        return priceVo;
    }
}
